package cn.iocoder.yudao.module.product.service.productmanagement;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.product.controller.admin.productmanagement.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.productmanagement.ProductManagementDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.product.convert.productmanagement.ProductManagementConvert;
import cn.iocoder.yudao.module.product.dal.mysql.productmanagement.ProductManagementMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.*;

/**
 * 产品 Service 实现类
 *
 * @author wcl
 */
@Service
@Validated
public class ProductManagementServiceImpl implements ProductManagementService {

    @Resource
    private ProductManagementMapper managementMapper;

    @Override
    public Long createManagement(ProductManagementCreateReqVO createReqVO) {
        // 插入
        ProductManagementDO management = ProductManagementConvert.INSTANCE.convert(createReqVO);
        managementMapper.insert(management);
        // 返回
        return management.getId();
    }

    @Override
    public void updateManagement(ProductManagementUpdateReqVO updateReqVO) {
        // 校验存在
        validateManagementExists(updateReqVO.getId());
        // 更新
        ProductManagementDO updateObj = ProductManagementConvert.INSTANCE.convert(updateReqVO);
        managementMapper.updateById(updateObj);
    }

    @Override
    public void deleteManagement(Long id) {
        // 校验存在
        validateManagementExists(id);
        // 删除
        managementMapper.deleteById(id);
    }

    private void validateManagementExists(Long id) {
        if (managementMapper.selectById(id) == null) {
            throw exception(MANAGEMENT_NOT_EXISTS);
        }
    }

    @Override
    public ProductManagementDO getManagement(Long id) {
        return managementMapper.selectById(id);
    }

    @Override
    public List<ProductManagementDO> getManagementList(Collection<Long> ids) {
        return managementMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ProductManagementDO> getManagementPage(ProductManagementPageReqVO pageReqVO) {
        return managementMapper.selectPage(pageReqVO);
    }

}
